package kr.saeildamil.androidstudy.ch12;

import java.util.ArrayList;

import kr.saeildamil.androidstudy.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyListAdapter extends BaseAdapter {
    Context mainContext;
    int icontextlayout;
    ArrayList<MyItem> arSrc;
    LayoutInflater Inflater;
	
	public MyListAdapter(Context mainContext, int icontextlayout,
			ArrayList<MyItem> arSrc) {
		this.mainContext = mainContext;
		this.icontextlayout = icontextlayout;
		this.arSrc = arSrc;
		this.Inflater = (LayoutInflater) mainContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return arSrc.size();
	}

	@Override
	public Object getItem(int position) {
		return arSrc.get(position).name;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int pos = position;
		if(convertView == null){
			convertView = Inflater.inflate(icontextlayout, parent, false);
		}
		ImageView img = (ImageView) convertView.findViewById(R.id.ch12_icontext_img);
		img.setImageResource(arSrc.get(position).iCon);
		
		TextView tv = (TextView) convertView.findViewById(R.id.ch12_icontext_tv);
		tv.setText(arSrc.get(position).name);
		
		Button btn = (Button) convertView.findViewById(R.id.ch12_icontext_btn);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String str = arSrc.get(pos).name + "를 주문합니다.";
				Toast.makeText(mainContext, str, Toast.LENGTH_SHORT).show();
				
			}
		});
		
		return convertView;
	}

}
